{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "2d2cc425-3c60-4172-93a4-bf5041ea0579",
   "metadata": {},
   "source": [
    "# 第九节、广播机制"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "02b2ad75-6161-4551-8875-0c3189775236",
   "metadata": {},
   "source": [
    "当两个数组的形状并不相同的时候，我们可以通过扩展数组的方法来实现相加、相减、相乘等操作，这种机制叫做广播"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a8ecfa9-c4e0-495c-a97f-794ed50fa450",
   "metadata": {},
   "source": [
    "## 一维数组广播"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "7bb4233b-4709-4c16-8b53-6b8660892a8f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6145609c-9011-4b38-943f-1e0e235064f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.array([0, 1, 2, 3]*3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "baf4271f-121a-4222-82a9-885758b5de64",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "c742b62a-7658-466b-884d-ca945b693a18",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = arr1.reshape(3, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "cb501b66-5939-44f4-9cd4-786bba4a3627",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 2, 3],\n",
       "       [0, 1, 2, 3],\n",
       "       [0, 1, 2, 3]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "e205358d-cc40-4e47-a27c-8f3d7c03d7ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(3, 4)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "86c90a67-4318-4e60-9abf-3ddac19c1dec",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4,)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2 = np.array(range(4))\n",
    "arr2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "602bfd78-3c8a-4f28-bb22-5af268b22258",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 2, 3])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f07c7841-0564-47f2-9c55-f9e350a3fdd8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 2, 4, 6],\n",
       "       [0, 2, 4, 6],\n",
       "       [0, 2, 4, 6]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 + arr2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cbd9f7af-b8f0-4e00-b0e3-8eb8c5351ace",
   "metadata": {},
   "source": [
    "## 二维数组的广播"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "02469bcb-bda1-4c24-a59e-4a5fd710cf55",
   "metadata": {},
   "outputs": [],
   "source": [
    "arr1 = np.sort(np.array([0, 1, 2, 3]*3)).reshape(4, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "255030b8-97cb-4d8d-881c-ef6b36af1865",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0],\n",
       "       [1, 1, 1],\n",
       "       [2, 2, 2],\n",
       "       [3, 3, 3]])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "5908e22d-6adc-4472-b0a4-e51441414552",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [2],\n",
       "       [3],\n",
       "       [4]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr2 = np.array([[1],[2],[3],[4]])\n",
    "arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "d7f41226-eb3f-4ae5-8135-5af4e01ba5c6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1],\n",
       "       [3, 3, 3],\n",
       "       [5, 5, 5],\n",
       "       [7, 7, 7]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr1 + arr2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f0782253-979c-468f-8846-b53194d57947",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
